package com.xwaf.platform.web.controller.x;

import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.xwaf.platform.common.api.CommonResponse;
import com.xwaf.platform.common.web.BaseController;
import com.xwaf.platform.system.api.request.auth.I18nRequest;
import com.xwaf.platform.web.config.LocaleMessage;

import io.swagger.annotations.Api;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/x/locale")
@ApiIgnore
@Api(hidden = true)
public class LocaleController extends BaseController {

	@Resource
	private LocaleMessage localeMessage;

	/**
	 * 语言切换
	 */
	@GetMapping("/checklanguage/{language}")
	public void language(HttpServletRequest request, HttpServletResponse response, @PathVariable String language) {
		// 打印日志
		Locale locale = request.getLocale();
		logger.error(locale.toString());
		LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
		language = language.toLowerCase();
		logger.info("language:" + language);
		if (language.equalsIgnoreCase("CN")) {
			localeResolver.setLocale(request, response, Locale.CHINA);// 中文
		} else if (language.equalsIgnoreCase("US")) {
			localeResolver.setLocale(request, response, Locale.US);// 英文
		} else if (language.equalsIgnoreCase("UY")) {
			localeResolver.setLocale(request, response, new Locale("es", "UY"));// 西班牙文
		} else {
			localeResolver.setLocale(request, response, Locale.CHINA);// 默认中文
		}
	}

	@GetMapping("/localeMessage")
	@ResponseBody
	public CommonResponse localeMessage() {
		System.out.println("i18n.auth.name: " + localeMessage.getMessage("i18n.auth.name"));
		System.out.println("module.title: " + localeMessage.getMessage("module.title"));
		System.out.println("title: " + localeMessage.getMessage("title"));
		System.out.println("test title: " + localeMessage.getMessage("test.title"));
		System.out.println("name: " + localeMessage.getMessage("name", new Object[] { "spring" }));
		System.out.println(localeMessage.getLocale());
		I18nRequest ir = new I18nRequest();
		ir.setI18nName("i18n.auth.name");
		ir.setAuthName("测试返回过程中的i18n值");
		ir.setHello("你好!!!");
		return CommonResponse.createCommonResponse(localeMessage.getI18nResult(ir));
	}
}
